package cn.lil.servlet;

import cn.lil.model.Bearing;
import cn.lil.service.BearingService;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
import org.apache.commons.fileupload2.core.DiskFileItem;
import org.apache.commons.fileupload2.core.DiskFileItemFactory;
import org.apache.commons.fileupload2.core.FileUploadException;
import org.apache.commons.fileupload2.jakarta.JakartaServletDiskFileUpload;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "BearingAddServlet", value = "/BearingAddServlet")
public class BearingAddServlet extends HttpServlet {
    BearingService bearingService = new BearingService();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        DiskFileItemFactory factory = new DiskFileItemFactory.Builder().get();
        JakartaServletDiskFileUpload upload = new JakartaServletDiskFileUpload(factory);
        upload.setHeaderCharset(Charset.defaultCharset());
        Charset charset = Charset.forName("UTF-8");
        try {
            List<DiskFileItem> list = upload.parseRequest(request);
            Bearing bearing = new Bearing();
            for (DiskFileItem item : list) {
                if (item.isFormField()) {
                    switch (item.getFieldName()) {
                        case "type":
                            bearing.setType(item.getString(charset));
                            break;
                        case "model":
                            bearing.setModel(item.getString(charset));
                            break;
                        case "data":
                            bearing.setData(Integer.parseInt(item.getString(charset)));
                            break;
                        case "life":
                            bearing.setLife(item.getString(charset));
                            break;
                        case "imgSrc":
                            bearing.setImgSrc(item.getString(charset));
                            break;
                        case "content":
                            bearing.setContent(item.getString(charset));
                            break;
                    }
                } else {
                    if (item.getInputStream().available() <= 0) continue;
                    String fileName = item.getName();
                    System.out.println("fileName" + fileName);
                    fileName = "/" + fileName;
                    System.out.println("fileName" + fileName);
                    String path = this.getServletContext().getRealPath("./assets/img/brearingimg") + fileName;
                    InputStream in = item.getInputStream();
                    FileOutputStream out = new FileOutputStream(path);
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while ((len = in.read(buffer)) > 0) {
                        out.write(buffer, 0, len);
                    }
                    in.close();
                    out.close();
                    item.delete();
                    bearing.setImgSrc("./assets/img/brearingimg" + fileName);
                    bearingService.addBearing(bearing);
                    request.getRequestDispatcher("/BearingServlet").forward(request, response);
                }
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
